from django.db.models import ForeignKey,BooleanField, CharField, DateField, TextField,DateTimeField, DecimalField, IntegerField,CASCADE,DO_NOTHING,PROTECT
from apps.vadmin.op_drf.models import CoreModel

'''
status值有 pending:待处理 archived:已存档 deployable:可部署 undeployable:不可部署
'''
class Assets(CoreModel):
    name = CharField(max_length=256,null=False,verbose_name="资产名称")
    asset_tag = CharField(max_length=32,null=True,unique=True,verbose_name="标签")
    model = ForeignKey(to="assets.Models",null=True,verbose_name="型号id",db_constraint=False,on_delete=PROTECT) # CASCADE 删除model中数据时，关联的数据都删除了
    serial = CharField(max_length=256,null=True,verbose_name="序列号")
    purchase_date = DateField(null=True,verbose_name="采购日期")
    asset_eol_date = DateField(null=True,verbose_name="寿命结束日期")
    purchase_cost = DecimalField(max_digits=20,decimal_places=2,null=True,verbose_name="采购价格")
    order_number = CharField(max_length=256,null=True,verbose_name="订单编号")
    assigned_to_id = IntegerField(null=True,verbose_name="借出目标Id")
    assigned_to_name = CharField(max_length=256,null=True,verbose_name="借出目标名称")
    assigned_type = CharField(max_length=32,null=True,verbose_name="借出类型")  # 借给 asset/user/location
    notes = TextField(null=True,verbose_name="备注")
    image = TextField(null=True,verbose_name="图片地址")
    physical = BooleanField(null=True,default=1,verbose_name="是否是物理设备")
    deleted_datetime = DateTimeField(null=True,verbose_name="删除时间")
    # status = ForeignKey(to="assets.StatusLabels",null=True,verbose_name="状态表id",on_delete=DO_NOTHING,db_constraint=False)
    status = CharField(max_length=32,null=True,verbose_name="状态")
    warranty_months = IntegerField(null=True,verbose_name="质保月份")
    depreciate = BooleanField(null=True,verbose_name="是否会折旧")
    supplier = ForeignKey(to="assets.Suppliers",null=True,verbose_name="供应商id",on_delete=PROTECT,db_constraint=False)
    requestable = BooleanField(max_length=4,null=False,default=0,verbose_name="是否可申请 1:可申请 0:不可")
    location = ForeignKey(to="assets.Locations",null=True,db_constraint=False,on_delete=PROTECT,verbose_name="关联位置")
    rtd_location_id = IntegerField(null=True,verbose_name="地点id")
    _snipeit_mac_address_1 = CharField(max_length=256,null=True,verbose_name="硬件物理mac地址")
    accepted = CharField(max_length=256,null=True,verbose_name="")  # 不知道什么意思
    last_checkout = DateTimeField(null=True,verbose_name="最后一次借出时间")
    expected_checkin = DateField(null=True,verbose_name="预期归还时间")
    last_audit_date = DateField(null=True,verbose_name="上次审计日期")
    next_audit_date = DateField(null=True,verbose_name="下次审计日期")
    checkin_counter = IntegerField(null=False,default=0,verbose_name="归还次数")
    checkout_counter = IntegerField(null=False,default=0,verbose_name="借出次数")
    requests_counter = IntegerField(null=False,default=0,verbose_name="申请次数")
    byod = BooleanField(null=True,default=0,verbose_name="是否是自带设备 1:是 0:否")

    class Meta:
        verbose_name = '资产'
        verbose_name_plural = verbose_name
        ordering = ['-id']  # 按id降序

    def __str__(self):
        return f"{self.name}"
